<template>
  <t-dialog :header="header" :visible.sync="visible" :onClose="closeDialog"     :closeOnOverlayClick="false"
            @confirm="onSubmit" :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }" width="60%"
            top="5%">
    <t-form ref="form" :data="formData" labelWidth="150px" :rules="rules">
      <t-row style="row-gap:24px">
        <t-col :span="12">
          <t-form-item label="分组名" name="name">
            <t-input style="width: 390px" v-model="formData.name" placeholder="请输入内容"></t-input>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="分组类型">
            <t-radio-group style="width: 390px" v-model="formData.reviewMenuType">
              <t-radio value="1"> 菜单</t-radio>
              <t-radio value="2"> 评审组</t-radio>
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col v-if="['2'].includes(formData.reviewMenuType)" :span="12">
          <t-form-item label="评审业务类型" name="reviewType">
            <t-select style="width: 390px" placeholder="评审业务类型" clearable  :options="dict.type.ggxq_review_type"
                      v-model="formData.reviewType">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col v-if="['2'].includes(formData.reviewMenuType)" :span="12">
          <t-form-item label="专家评审开始时间"  name="startReviewDate">
            <t-date-picker  allow-input clearable  style="width: 390px" v-model="formData.startReviewDate"></t-date-picker>
          </t-form-item>
        </t-col>
        <t-col v-if="['2'].includes(formData.reviewMenuType)" :span="12">
          <t-form-item label="专家评审截止时间"  name="endReviewDate">
            <t-date-picker  allow-input clearable  style="width: 390px" v-model="formData.endReviewDate"></t-date-picker>
          </t-form-item>
        </t-col>
        <t-col v-if="['2'].includes(formData.reviewMenuType)" :span="12">
          <t-form-item label="评审会议时间"  name="reviewMeetingDate">
            <t-date-picker  allow-input clearable  style="width: 390px" v-model="formData.reviewMeetingDate"></t-date-picker>
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item label="序号" name="sort">
            <t-input-number style="width: 390px" clearable v-model="formData.sort" :min="1" placeholder="请输入"></t-input-number>
          </t-form-item>
        </t-col>
         <t-col  v-if="['2'].includes(formData.reviewMenuType)"  :span="12">
          <t-form-item label="本评审组限制最大推荐数" name="recommendNum">
            <t-input-number style="width: 390px" clearable v-model="formData.recommendNum" :min="0" ></t-input-number>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="分组说明" name="note">
            <t-textarea  :autosize="{minRows: 5}" v-model="formData.note"></t-textarea>
          </t-form-item>
        </t-col>

      </t-row>
    </t-form>


  </t-dialog>
</template>

<script>

export default {
  name: "add-edit-group",
  dicts: ['ggxq_review_type'],
  components: {
  },
  props: ['header', 'objId', 'parentId'],
  data() {
    return {
      dialog: {
        header: '',
        objId: 0,
        open: false,
      },
      menuOptions: [],
      visible: true,
      loading: false,
      formData: {
        parentId: null,
        reviewType: '',
        name: '',
        sort: 1,
        note: '',
        indexId: "",
        subjectClassification:[],
        reviewMenuType: '1',
        startReviewDate: '',
        endReviewDate: '',
        reviewMeetingDate: '',
        recommendNum:'',
      },
      rules: {
        parentId: [
          {required: true, message: "上级分组ID不能为空"}
        ],
        name: [
          {required: true, message: "分组名称不能为空"}
        ],
        sort: [
          {required: true, message: "显示排序不能为空"}
        ],
        subjectClassification: [
          {required: true,validator: (val) => val.length>0, message: "学科分类不能为空"}
        ],
        reviewType: [
          {required: true, message: "评审业务类型不能为空"}
        ],
        startReviewDate: [ {required: true, message: "专家评审开始时间不能为空"}],
        endReviewDate: [
          {
            required: true,
            message: "专家评审截止时间不能为空",
          },
          {
            validator: (val) => {
              if (val && this.formData.startReviewDate) {
                const startTime = new Date(this.formData.startReviewDate).getTime()
                const endTime = new Date(val).getTime()
                return startTime <= endTime;
              }
              return true;
            }, message: "评审开始时间不能大于评审截止时间", type: "error"
          },
        ]
      },
      dataOptions:[],
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
        departmentName: "",
      },
    }
  },
  watch: {
    // objId: {
    //   immediate: true,
    //   handler(val) {
    //     if (val) {
    //       this.getObj(val);
    //     }
    //   }
    // },
    parentId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.formData.parentId = val;
        }
      }
    }
  },
  async created() {
    if(this.objId!==0){
      this.getObj();
    }
  },
  methods: {
    getObj() {
      this.$api.ggxq.review.teamGet(this.objId).then((response) => {
        // 选中行排前面
        // this.formData.selectedRowKeys = [response.data.indexId];
        // const index = this.data.findIndex((val) => val.id === this.formData.selectedRowKeys[0]);
        // const newArr = this.data.splice(index,1);
        // this.data.unshift(newArr[0]);
        response.data.subjectClassification = response.data.subjectClassification?response.data.subjectClassification.split(",").filter(item=> item !== ''):[]
        Object.assign(this.formData, response.data);
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId !== 0) {
            if (this.objId === this.formData.parentId) {
              this.formData.parentId = 0;
            }
            this.$api.ggxq.review.teamUpdate({
              ...this.formData,
              subjectClassification: this.formData.subjectClassification?.join(",")}
            ).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              if(this.formData.reviewMenuType==='2'){
                this.$emit("changeActived", this.objId);
              }
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            let {parentId} = this.formData;
            if (!this.formData.parentId) {
              parentId = 0;
            }
            this.$api.ggxq.review.teamAdd({...this.formData, parentId,
              subjectClassification: this.formData.subjectClassification.join(",")
            }).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              if(this.formData.reviewMenuType==='2'){
                this.$emit("changeActived", response.data.id);
              }
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },

  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
